package cn.jiyun.controller;

import cn.jiyun.entity.*;

import cn.jiyun.service.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author weiyuanhang
 * @data: 2023/8/23 002316:40
 * @Package cn.jiyun.controller
 * @email 3185757326@qq.com
 */
@RestController
@RequestMapping("/mycourse")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseFigenService courseFigenService;
    @Autowired
    private ContestFigenService contestFigenService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CourseBlaeFeginService courseBlaeFeginService;
    @Autowired
    private DirectoryFeginService directoryFeginService;


    @GetMapping("/findBaCourse")
    public Integer findBaCourse() {
        return courseFigenService.findCountId();
    }

    @GetMapping("/findByContest")
    public Integer findByContest() {
        return contestFigenService.findByCount();
    }

    @GetMapping("/findByCourse")
    public List<Course> findByCourse() {
        return courseFigenService.findCourse();
    }

    @GetMapping("/findNum")
    public Result findNum(Integer cid) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", cid);
        Course one = courseService.getOne(wrapper);

        if (one.getNums() == null) {
            Course course = new Course();
            course.setNums(1);
            courseService.save(course);
        } else {
            one.setNums(one.getNums() + 1);
            courseService.updateById(one);
        }
        Integer nums = (Integer) redisTemplate.opsForHash().get("点赞数量", String.valueOf(cid));

        if (nums == null) {
            redisTemplate.opsForHash().put("点赞数量", String.valueOf(cid), 1);
        } else {
            redisTemplate.opsForHash().put("点赞数量", String.valueOf(cid), nums + 1);
        }
        return Result.ok(1);
    }

    //排序最新课程
    @GetMapping("/findByLastCourse")
    public List<Course> findByLastCourse() {
        return courseFigenService.findLastCourse();
    }

    @GetMapping("/findCounts")
    public Result findCounts(Integer cid){
        Course course = (Course) redisTemplate.opsForHash().get("点赞数量",cid);
//        Course course = JSONObject.parseObject(String.valueOf(qwe), Course.class);
        if (cid.equals(course.getCid())){
            course.setNums(course.getNums() - 1);
            courseService.updateById(course);
        }else {
            course.setNums(course.getNums()+1);
            courseService.updateById(course);
        }
        redisTemplate.opsForHash().put("点赞数量", String.valueOf(cid), JSONArray.toJSONString(course));
        return Result.ok(1);
    }

    //点赞
    @GetMapping("/addz")
    public Result addz(Integer cid, HttpServletRequest request){

        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", cid);
        Course one = courseService.getOne(wrapper);

        if (one.getNums() == null) {
            Course course = new Course();
            course.setNums(1);
            courseService.save(course);
        } else {
            one.setNums(one.getNums() + 1);
            courseService.updateById(one);
        }


        String s = (String) redisTemplate.opsForHash().get("点赞数量",cid.toString());
        if (!StringUtils.isBlank(s)){
            String token = request.getHeader("token");
            String s1 = (String) redisTemplate.opsForValue().get(token);
            Menber menber = JSONObject.parseObject(s1, Menber.class);
            if (s.contains("mid"+menber.getMid())){
                String s2 = s.replace("mid" + menber.getMid(), "");
                redisTemplate.opsForHash().put("点赞数量", cid.toString(), s2);
                return Result.ok(1);
            }else {
                String s2=s+"mid"+menber.getMid();
                redisTemplate.opsForHash().put("点赞数量", cid.toString(), s2);
                return Result.ok(2);
            }
        }else {
            redisTemplate.opsForHash().put("点赞数量",cid.toString(),1);
        }
        return Result.ok(3);
    }

    //课程列表
    @RequestMapping("/findAllCourse")
    public PageInfo<CourseVO> findAllCourse(@RequestBody CourseDTO courseDTO){
        return courseFigenService.CourseFindAll(courseDTO);
    }

    @GetMapping("/findBlae")
    public Result findBlae(){
        return   courseBlaeFeginService.findByBlae();
    }
    @GetMapping("/findHoCourse")
    public List<Course> findHottestCourse(){
        return courseFigenService.findHottestCourse();
    }

    @GetMapping("/xueDirectory")
    private ResultClass xueDirectory(Integer cid, Integer mid, Integer did){
        return directoryFeginService.xueDirectory(cid,mid,did);
    }
/*1111111111111111111111*/

    @GetMapping("/findTowDitectoryNum")
    private ResultClass findTowDitectoryNum(Integer cid){
        Integer num=directoryFeginService.findTowDitectoryNum(cid);
        return ResultClass.ok(num);
    }
    @GetMapping("/findUserOverDirectory")
    private ResultClass findUserOverDirectory(Integer cid,Integer mid){
        List<CourseDistory> list=directoryFeginService.findOverDirectoryNum(cid,mid);
        return ResultClass.ok(list);
    }

    @GetMapping("/findDitectoryList")
    private ResultClass findDitectoryList(Integer cid){
        List<DistoryVO> ditectoryList = directoryFeginService.findDitectoryList(cid);
        return ResultClass.ok(ditectoryList);
    }

}
